import $http from "@/api/fetch";
import api from '@/api/api';
import { Encrypt } from "@/utils/sign";
import Cookies from 'js-cookie';
import Util from '@/utils/util'
import { appRouter, dataRouter, routers } from '@/router/router'
/**
 * 登录，退出操作
 * 
 **/
const app = {
    state: {
        cachePage: [],
        mobile: false,
        lang: '',
        isFullScreen: false,
        openedSubmenuArr: [], // 要展开的菜单数组
        menuTheme: 'dark', // 主题
        themeColor: '',
        pageOpenedList: [],
        currentPageName: '',
        currentPath: [], // 面包屑数组
        menuList: [],
        routers: [
            ...appRouter,
            ...dataRouter

        ],
        tagsList: [routers], //...otherRouter.children
        messageCount: 0,
        dontCache: ['text-editor', 'artical-publish'] // 在这里定义你不想要缓存的页面的name属性值(参见路由配置router.js)
    },
    mutations: {
        setMobile(state, flag) {
            state.mobile = flag;

        },
        setTagsList(state, list) {
            state.tagsList.push(...list);
        },
        updateMenulist(state) {

            var ids = [];
            if (localStorage.userInfo != null && localStorage.userInfo != undefined) {
                let authMenus = JSON.parse(localStorage.userInfo).authMenus;

                if (authMenus)
                    authMenus.forEach((it) => {
                        ids.push(it.name);
                    });
            }

            let menuList = [];
            //routers appRouter
            let routers = [
                ...appRouter,
                ...dataRouter

            ];
            routers.forEach((item, index) => {
                
                if (item.children.length === 1) {

                    if (ids.indexOf(item.children[0].name) != -1 || ids.indexOf(item.name) != -1) {
                        menuList.push(item);
                    }
                    // menuList.push(item);
                } else {
                    let len = menuList.push(item);
                    let childrenArr = [];
                    childrenArr = item.children.filter(child => {

                        // return ids.indexOf(child.name) != -1;
                        return true;
                    });

                    if (childrenArr === undefined || childrenArr.length === 0) {
                        menuList.splice(len - 1, 1);
                    } else {
                        let handledItem = JSON.parse(JSON.stringify(menuList[len - 1]));
                        handledItem.children = childrenArr;
                        menuList.splice(len - 1, 1, handledItem);
                    }
                }

            });

            state.menuList = menuList;
        },
        changeMenuTheme(state, theme) {
            state.menuTheme = theme;
        },
        changeMainTheme(state, mainTheme) {
            state.themeColor = mainTheme;
        },
        addOpenSubmenu(state, name) {


            let hasThisName = false;
            let isEmpty = false;
            if (name.length === 0) {
                isEmpty = true;
            }
            if (state.openedSubmenuArr.indexOf(name) > -1) {
                hasThisName = true;
            }
            if (!hasThisName && !isEmpty) {
                state.openedSubmenuArr.push(name);
            }
        },
        closePage(state, name) {
            state.cachePage.forEach((item, index) => {
                if (item === name) {
                    state.cachePage.splice(index, 1);
                }
            });
        },
        initCachepage(state) {
            if (localStorage.cachePage) {
                state.cachePage = JSON.parse(localStorage.cachePage);
            }
        },
        removeTag(state, name) {
            state.pageOpenedList.map((item, index) => {
                if (item.name === name) {
                    state.pageOpenedList.splice(index, 1);
                }
            });
        },
        pageOpenedList(state, get) {
            let openedPage = state.pageOpenedList[get.index];
            if (get.argu) {
                openedPage.argu = get.argu;
            }
            if (get.query) {
                openedPage.query = get.query;
            }
            state.pageOpenedList.splice(get.index, 1, openedPage);
            localStorage.pageOpenedList = JSON.stringify(state.pageOpenedList);
        },
        clearAllTags(state) {
            // state.pageOpenedList.splice(0);
             let currentIndex = 0;
             state.pageOpenedList.forEach((item, index) => {
                if (item.name === "Index"
                 ||item.name === "PmIndex" || item.name === "YewuIndex") {
                    currentIndex = index;
                }
            });
            if(currentIndex==0)
            {
                state.pageOpenedList.splice(1);
            }
            else{
                state.pageOpenedList.splice(currentIndex + 1);
                state.pageOpenedList.splice(0, currentIndex - 1);
            }
            state.cachePage.length = 0;
            localStorage.pageOpenedList = JSON.stringify(state.pageOpenedList);
        },
        clearOtherTags(state, vm) {
            let currentName = vm.$route.name;
            let currentIndex = 0;
            state.pageOpenedList.forEach((item, index) => {
                if (item.name === currentName) {
                    currentIndex = index;
                }
            });
            if (currentIndex === 0) {
                state.pageOpenedList.splice(1);
            } else {
                state.pageOpenedList.splice(currentIndex + 1);
                state.pageOpenedList.splice(0, currentIndex - 1);
            }
            let newCachepage = state.cachePage.filter(item => {
                return item === currentName;
            });
            state.cachePage = newCachepage;
            localStorage.pageOpenedList = JSON.stringify(state.pageOpenedList);
        },
        setOpenedList(state) {
            let pages = [

            ];
            // debugger;
            for (var i = 0; i < state.menuList.length; i++) {
                let child = state.menuList[i];

                if (child.children.length <= 0) {
                    if (child.name == "Index" || child.name == "PmIndex" || child.name == "YewuIndex") {
                        // path = "/index"
                        pages.push({ name: child.name, title: "工作台" });
                        break;
                    }
                } else {
                    for (var j = 0; j < child.children.length; j++) {
                        let hd = child.children[j];
                        if (hd.name == "Index" || hd.name == "PmIndex" || hd.name == "YewuIndex") {
                            pages.push({ name: hd.name, title: "工作台" });
                            break;
                        }
                    }
                    if (pages.length > 0) {
                        break;
                    }
                }
            }
            // debugger;
            state.pageOpenedList = localStorage.pageOpenedList ?
                JSON.parse(localStorage.pageOpenedList) : pages;
        },
        setCurrentPath(state, pathArr) {
            state.currentPath = pathArr;
        },
        setCurrentPageName(state, name) {
            state.currentPageName = name;
        },
        setAvator(state, path) {
            localStorage.avatorImgPath = path;
        },
        switchLang(state, lang) {
            state.lang = lang;
            Vue.config.lang = lang;
        },
        clearOpenedSubmenu(state) {
            state.openedSubmenuArr.length = 0;
        },
        setMessageCount(state, count) {
            state.messageCount = count;
        },
        increateTag(state, tagObj) {
            if (!Util.oneOf(tagObj.name, state.dontCache)) {
                state.cachePage.push(tagObj.name);
                localStorage.cachePage = JSON.stringify(state.cachePage);
            }
            state.pageOpenedList.push(tagObj);
            localStorage.pageOpenedList = JSON.stringify(state.pageOpenedList);
        }
    },
    actions: {

    }
}

export default app